package com.wht.arthas.service.impl;

import com.wht.arthas.model.Role;
import com.wht.arthas.model.User;
import com.wht.arthas.service.UserService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author haoting.wang
 * @date 2020/8/4 7:23 下午
 **/
@Service
public class UserServiceImpl implements UserService {

    private static Map<Integer, User> map = new HashMap<>(8);

    static {
        map.put(1, new User(1, "wht", 20));
        map.put(2, new User(2, "wanght", 21));
        map.put(3, new User(3, "whaot", 21));
        map.put(4, new User(4, "whting", 20));
        map.put(5, new User(5, "wanghaot", 22));
        map.put(6, new User(6, "wanghting", 21));
        map.put(7, new User(7, "whaoting", 23));

        User user = new User(8, "wanghaoting", 18);

        List<Role> roles = new ArrayList();
        Role role = new Role(1, "admin");
        roles.add(role);
        user.setRoles(roles);

        map.put(8, user);
    }

    @Override
    public User findById(Integer id) {

        return map.get(id);
    }

    @Override
    public Collection<User> getAllUser() {
        return map.values();
    }

    @Override
    public void saveOrUpdate(User user) {
        map.put(user.getId(), user);
    }

    @Override
    public void update(User user) {
        map.put(user.getId(), user);
    }

    @Override
    public void save(User user) {
        map.put(user.getId(), user);
    }

    @Override
    public void sleep() {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
